package jvm;

import java.util.Random;

public class Test02 {
    public static void main(String[] args) {
        //返回java虚拟机试图使用的最大内存量
        /*
        修改jvm虚拟机参数
       idea -》 run ->Edit Configurations  -> VM options   ->
       -Xms1024m   -Xms初始空间的大小，设置初始空间和最大空间一致，减少波动
       -Xmx1024m   -Xmx最大内存空间的大小
       -XX:+PrintGCDetails  -XX:PrintGCDetails
         */
        long maxMemory = Runtime.getRuntime().maxMemory();
        System.out.println("MAX_MEMORY = " + maxMemory +"字节");//java8默认最大内存占物理内存的 1/4
        System.out.println("MAX_MEMORY = " + maxMemory + "(字节)" + (maxMemory / (double)1024 / 1024) + "MB");
        System.out.println("----------------------------------");
        //返回java虚拟机中的内存总量
        long totalMemory = Runtime.getRuntime().totalMemory();
        System.out.println("TOTAL_MEMORY  = " + totalMemory +"字节");//java8 total内存默认占物理内存的 1/64
        System.out.println("TOTAL_MEMORY = " + totalMemory + "(字节)" + (totalMemory / (double)1024 / 1024) + "MB");

        System.out.println("CPU的核心数：" + Runtime.getRuntime().availableProcessors());
        //Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        String s = "abc";
        while (true) {
            s += s + new Random().nextInt(888888) + new Random(999);
        }
        /*
        //内存分配失败
        [GC (Allocation Failure) [PSYoungGen: 1985K->489K(2560K)] 1985K->777K(9728K), 0.0025989 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
        [GC (Allocation Failure) [PSYoungGen: 2432K->424K(2560K)] 2720K->1381K(9728K), 0.0011267 secs] [Times: user=0.02 sys=0.01, real=0.00 secs]
        [GC (Allocation Failure) [PSYoungGen: 2068K->424K(2560K)] 4096K->2987K(9728K), 0.0008971 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
        [Full GC (Ergonomics) [PSYoungGen: 2069K->0K(2560K)] [ParOldGen: 6847K->2805K(7168K)] 8916K->2805K(9728K), [Metaspace: 3264K->3264K(1056768K)],
            0.0052061 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
        [GC (Allocation Failure) [PSYoungGen: 41K->0K(2560K)] 4988K->4947K(9728K), 0.0005766 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
        [GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 4947K->4947K(9728K), 0.0004535 secs] [Times: user=0.03 sys=0.00, real=0.00 secs]
        [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 4947K->3875K(7168K)] 4947K->3875K(9728K),
                [Metaspace: 3264K->3264K(1056768K)], 0.0032959 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
        [GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 3875K->3875K(8704K), 0.0003855 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
        [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 3875K->3856K(7168K)] 3875K->3856K(8704K),
            [Metaspace: 3264K->3264K(1056768K)], 0.0069879 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
        Heap
         PSYoungGen      total 1536K, used 60K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
          eden space 1024K, 5% used [0x00000000ffd00000,0x00000000ffd0f2f8,0x00000000ffe00000)
          from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
          to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
         ParOldGen       total 7168K, used 3856K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
          object space 7168K, 53% used [0x00000000ff600000,0x00000000ff9c4038,0x00000000ffd00000)
         Metaspace       used 3296K, capacity 4500K, committed 4864K, reserved 1056768K
          class space    used 355K, capacity 388K, committed 512K, reserved 1048576K
         */
    }
}
